package com.itcam.lease.web.admin.service.impl;

import com.itcam.lease.model.entity.*;
import com.itcam.lease.web.admin.mapper.*;
import com.itcam.lease.web.admin.service.LeaseAgreementService;
import com.itcam.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.itcam.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired

    private RoomInfoMapper roomInfoMapper;
    @Autowired

    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 根据条件分页查询租约列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.pageAgreementByQuery(page, queryVo);
    }

    /**
     * 根据id查询租约信息
     * @param id
     * @return
     */
    @Override
    public AgreementVo getAgreementById(Long id) {
        //1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
        //3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        //4.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        //5.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        // 创建一个新的 AgreementVo 对象，用于封装租约的详细信息
        AgreementVo adminAgreementVo = new AgreementVo();
        // 使用 Apache Commons BeanUtils 库的 copyProperties 方法，将 leaseAgreement 对象的属性复制到 adminAgreementVo 对象中
        BeanUtils.copyProperties(leaseAgreement, adminAgreementVo);
        // 设置公寓信息
        adminAgreementVo.setApartmentInfo(apartmentInfo);
        // 设置房间信息
        adminAgreementVo.setRoomInfo(roomInfo);
        // 设置支付方式
        adminAgreementVo.setPaymentType(paymentType);
        // 设置租期
        adminAgreementVo.setLeaseTerm(leaseTerm);
        // 返回封装好的租约详细信息对象
        return adminAgreementVo;

    }
}




